<?php

class indexAction extends frontendAction {

    //微信登录
    function weixin_login(){
        $this->visitor->is_login && $this->redirect('/');

        if(isset($_GET['state']) && $_GET['state']){   //此参数来自微信，如果有值，说明是在微信公众平台中打开的
            //微信回调,openid
            $code = $_GET['code'];
            $weixinAccess = M('access');

            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".C('WEIXIN_APPID')."&secret=".C('WEIXIN_APPSECRET')."&code=".$code."&grant_type=authorization_code";
            $json_res = json_decode($this->send_get_req($url),true);
            if(isset($json_res['errcode'])){
                $this->_404('/');
            }

            $access = $weixinAccess->where("openid='{$json_res['openid']}'")->find();
            if($access){   //已存在此用户，直接登录
                $this->visitor->login($access['uid'], 1);
                $this->redirect('/');
            }

            $getUserUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$json_res['access_token']."&openid=".$json_res['openid']."&lang=zh_CN";
            $json_res = json_decode($this->send_get_req($getUserUrl),true);
            if(!isset($json_res['errcode'])){
                $user = D('user');
                $data = array('username'=>$json_res['nickname'],
                        'gender'=>$json_res['sex'],
                        'avatar'=>$json_res['headimgurl'],
                        'province'=>$json_res['province'],
                        'city'=>$json_res['city'],
                    );
                $uid = $user->add($data);

                $data = array(
                        'openid'   => $json_res['openid'],
                        'userinfo' => json_encode($json_res),
                        'uid'      => $uid
                    );
                $res = $weixinAccess->add($data);
                if($res){
                    $this->redirect("/");
                }else{
                    echo "微信登录失败";exit;
                }

            }
        }else{
            echo "请在微信中打开";
        }
    }
    
    public function index() {
        header('location:/zd');exit;
        //$this->display();
        //redirect(u('index/zaodian'));
    	 
    	/*****首页广告***/
    	// $ad= M('ad');
    	// $ads= $ad->field('url,content,desc')->where('board_id=1 and status=1')->order('ordid asc')->select();
     //    $this->assign('ad',$ads);
     //    /*****首页广告end******/
        
        
     //    /****最新商品*****/
     //    $wherenews=array('news'=>1);
     //    $news=$this->getItem($wherenews);
     //     /****最新商品 END*****/
         
     //      /****推荐商品*****/
     //    $wherenews=array('tuijian'=>1);
     //    $tuijian=$this->getItem($wherenews);
      
     //    $this->assign('news',$news);
     //    $this->assign('tuijian',$tuijian);
     //    $this->_config_seo();
     //    $this->display();
    }

    function shaodaishou(){
        //SEO
        $this->_config_seo(C('pin_seo_config.cate'), array(
            'seo_title' => '捎带手',
        ));
        $where = array();
        $catIds = M('item_cate')->where(array('pid'=>C('SHAODAISHOU_CATE_ID')))->select();
        $ids = array(C('SHAODAISHOU_CATE_ID'));
        foreach ($catIds as $value) {
            $ids[] = $value['id'];
        }
        $where['cate_id']  = array('in',implode(",", $ids));
        $this->data = $this->getItem($where);
        $this->display();
    }

    function zaodian(){
        //SEO
        $this->_config_seo(C('pin_seo_config.cate'), array(
            'seo_title' => '早点约我',
        ));
        $where = array('cate_id'=>C('ZAOCAN_CATE_ID'));
        $this->data = $this->getItem($where);
        $this->display();
    }

    public function getItem($where = array())
    {
    	 $where_init = array('status'=>'1');
        $where =array_merge($where_init, $where);
    	
    	return $item=M('item')->where($where)->order('ordid')->select();
    }
    
    
    public function ajaxLogin()
    {
    	
        $user_name=$_POST['user_name'];
       $password=$_POST['password'];
       
       $user=M('user');
       $users= $user->where("username='".$user_name."' and password='".md5($password)."'")->find(); 
       if(is_array($users))
       {
    	$data = array('status'=>1);
    	$_SESSION['user_info']=$users;
       }else {
       	$data = array('status'=>0);
       }
    	
    	echo json_encode($data);
    	exit;
    }
    public function ajaxRegister()
    {
    	$username=$_POST['user_name'];
    	$user=M('user');
    	$count=$user->where("username='".$username."'")->find();
    	if(is_array($count))
    	{
        echo 'false';
       // echo json_encode(array('user_nameData'=>true));
    	}else 
    	{
    		echo 'true';
        //echo json_encode(array('user_nameData'=>true));
    	}
    	
    
    }

    function notice(){
        $this->notice = $this->_get('notice');
        $this->display();
    }
    
}